﻿/**
 * Created by easy on 15/5/11.
 */

(function () {
    service.url = "https://qc.tradeicloud.com/QC";//"http://demo.tradeicloud.com/QC"; //"http://192.168.1.132:3001";//"http://110.80.35.58:23801";

    // 获取订单数据
    service.getOrdersData = function (options, callback) {
        var sql = "select * from QC_ORDER where sellerID = ? and status = '进行中' and orderNo like '%" + options.orderNo + "%' order by lastUpdateTime desc limit ?,?";
        var sellerID = options.sellerID;
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [sellerID, (page - 1) * size, size];

        mc.execSQL(sql, params, function (data) {
            service.getOrderAbnormalAllUnreadCount(vars.sellerID, function (abnormal) {
                for (var j1 in data) {
                    for (var i1 in abnormal) {
                        if (data[j1].jsonPK == abnormal[i1].jsonPK)
                            data[j1].abnormalCount = abnormal[i1].count;
                    }
                }
                service.getOrderRemarkAllUnreadCount(vars.sellerID, function (remark) {
                    for (var j2 in data) {
                        for (var i2 in remark) {
                            if (data[j2].jsonPK == remark[i2].jsonPK)
                                data[j2].remarkCount = remark[i2].count;
                        }
                    }
                    callback(data)
                })
            })

        });
    };

    // 获取工厂数据
    service.getOrderPartysData = function (callback) {
        var sql = "select a.* from QC_PARTY a left JOIN QC_ORDER b on a.sellerID = b.sellerID where a.jsonPK != 'null' and b.status = '进行中' group by a.sellerID";
        mc.execSQL(sql, [], function (data) {
            var data2 = [];
            var dataLength = data.length;
            for (var i = 0; i < dataLength; i++) {
                if (data[i].jsonPK == "null") {
                    data.splice(i, 1);
                }
            }
            //alert(JSON.stringify(data));
            callback(data);
        });
    };

    // 获取异常数据
    service.getAbnormalsData = function (options, callback) {
        //jsonPK,orderID,description,status,completeRate,createdTime,createdByUserLogin,lastModifiedByUserLogin,lastUpdateTime
        var sql = "select a.*,b.attachment as orderAttachment,b.orderNo as orderNo,b.itemNo as itemNo,c.jsonPK as processID,c.processName as processName from QC_ABNORMAL a left join QC_ORDER b on a.orderID = b.jsonPK left join QC_PROCESS c on a.processID = c.jsonPK where a.status = '进行中' and b.status = '进行中' order by a.lastUpdateTime desc limit ?,?";
        //var sql = "select * from QC_ABNORMAL limit ?,?"
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [(page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            service.getAbnormalUnreadCount(function (abnormal) {
                //alert(JSON.stringify(abnormal))
                for (var i in data) {
                    for (var j in abnormal) {
                        if (data[i].jsonPK == abnormal[j].jsonPK) {
                            data[i].count = abnormal[j].count;
                        }
                    }
                }
                service.getAbnormalUnreadALL(function (jsonStr, abn) {
                    service.getAbnormalRemarkUnreadALL(jsonStr, function (remark) {
                        abn.count += parseInt(remark[0].count);
                        if (abn.count != 0) {
                            mc.getMessageCount("abnormal", abn.count);
                        } else {
                            mc.getMessageCount("abnormal", '');
                        }
                    });
                    callback(data);
                })
            });
        });
    };

    // 获取异常详情
    service.getAbnormalDetailData = function (abnormalID, callback) {
        var sql = "select a.*,b.attachment as orderAttachment,b.orderNo as orderNo,b.itemNo as itemNo,c.jsonPK as processID,c.processName as processName from QC_ABNORMAL as a left join QC_ORDER as b on a.orderID = b.jsonPK left join QC_PROCESS c on a.processID = c.jsonPK where a.jsonPK = ?"
        mc.execSQL(sql, [abnormalID], function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        });
    };

    // 获取异常备注
    service.getAbnormalRemarkData = function (options, callback) {
        var sql = "select a.*, b.status as abnormalStatus from QC_REMARK a left join  QC_ABNORMAL b on a.objID = b.jsonPK where a.objID = ? order by lastUpdateTime asc limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var abnormalID = options.abnormalID;
        var params = [abnormalID, (page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        });
    };

    // 获取异常备注回复
    service.getRemarkReplysData = function (str, callback) {
        var sql = "select * from QC_REMARK where objID in (" + str + ");";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        });
    };

    // 获取订单流程备注
    service.getOrderProcessRemarkData = function (options, callback) {
        var sql = "select a.*,b.processName as processName,c.jsonPK as orderID,c.attachment as orderAttachment, c.orderNo,c.itemNo from QC_REMARK a left join QC_PROCESS b on a.objID = b.jsonPK left join QC_ORDER c on a.orderID = c.jsonPK where type = 1" + (options.orderID ? " and a.orderID = '" + options.orderID + "'" : "") + (options.processID ? " and a.objID='" + options.processID + "'" : "") + " and c.status = '进行中' order by a.lastUpdateTime desc limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var orderID = options.orderID;
        var params = [(page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data))
            var jsonPKStr = "";
            for (var i = 0; i < data.length; i++) {
                jsonPKStr += ("'" + data[i].jsonPK + "'");
                if (i != data.length - 1) {
                    jsonPKStr += ",";
                }
            }
            // 获取备注回复
            service.getRemarkReplysData(jsonPKStr, function (replysData) {
                //alert(JSON.stringify(replysData))
                var remarkData = [];
                for (var i = 0; i < data.length; i++) {
                    var replyList = [];
                    for (var j = 0; j < replysData.length; j++) {
                        if (data[i].jsonPK == replysData[j].objID) {
                            replysData[j].processName = data[i].processName;
                            replyList.push(replysData[j]);
                        }
                    }
                    if (replyList != []) {
                        data[i].replyList = replyList.sort(//倒序

                            function (a, b) {

                                return (a.lastUpdateTime - b.lastUpdateTime);

                            }

                        );
                    }
                    if (orderID) {
                        if (data[i].type == 1 && data[i].orderID == orderID) {
                            remarkData.push(data[i]);
                        }
                    } else {
                        if (data[i].type == 1) {
                            remarkData.push(data[i]);
                        }
                    }
                }
                //alert(JSON.stringify(remarkData))
                if (remarkData.length == 0) {
                    callback(data);
                } else {
                    callback(remarkData);
                }
            });
        });
    };

    // 获取流程数据
    service.getOrderProcessData = function (orderID, callback) {
        var sql = "select * from QC_PROCESS where orderID = ? order by sno asc";
        mc.execSQL(sql, [orderID], function (data) {
            callback(data);
        })
    };

    // 获取流程异常
    service.getOrderProcessAbnormalsData = function (options, callback) {
        var sql = "select a.*,b.processName as processName from QC_ABNORMAL a left join QC_PROCESS b on a.processID = b.jsonPK where a.orderID = ? " + (options.processID ? " and processID='" + options.processID + "'" : "") + " order by a.lastUpdateTime desc limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var orderID = options.orderID;
        var params = [orderID, (page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        });
    }
    service.getAjaxData = function (options, callback) {
        var sql = "select * from T_CloudDataTransferCenter order by FCloudDataTransferCreatedTimeStamp desc limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [(page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        });
    };
    // 获取订单明细
    service.getOrderDetailData = function (options, callback) {
        var sql = "select a.remark,a.orderNo,a.itemNo,a.quantity,a.exFactoryDate,a.attachment,a.customerName,a.customerNo,b.sellerName,b.buyerName from QC_ORDER a left join QC_PARTY b on a.sellerID = b.sellerID where a.jsonPK = ?";

        //var sql = "select * from QC_ORDER where jsonPK = ?"
        var orderID = options.orderID;
        var params = [orderID];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            data[0].orderAttachment = data[0].attachment;
            callback([data[0]]);
        });
    };
    // 获取子流程信息
    service.getSubProcessData = function (options, callback) {
        var sql = "select * from QC_SUBPROCESS where processID = ?";
        var processID = options.processID;
        var params = [processID];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        });
    };

    // 获取流程备注
    service.getProcessRemarkData = function (options, callback) {
        var sql = "select * from QC_REMARK where type = 1 and objID = ?";
        var processID = options.processID;
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [processID];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        });
    };

    // 获取动态
    service.getDynamicData = function (options, callback) {
        var sql = "select a.*,b.attachment as orderAttachment,b.orderNo as orderNo,b.itemNo as itemNo from QC_DYNAMIC a join QC_ORDER b on a.orderID = b.jsonPK order by lastUpdateTime desc limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [(page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        });
    };
    //统计异常未读信息
    service.getAbnormalUnreadCount = function (callback) {
        var sql = "select unread,jsonPK from QC_ABNORMAL where status != '已完成' group by jsonPK";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data));
            service.getAbnormalRemarkUnreadCount(function (dataCount) {
                var abnormal = [];
                for (var i in data) {
                    if(data[i].unread == "T") {
                        data[i].count = 1;
                    } else {
                        data[i].count = 0;
                    }
                    for (var j in dataCount) {
                        if (dataCount[j].jsonPK == data[i].jsonPK) {
                            data[i].count += parseInt(dataCount[j].count);
                        }
                    }
                    if(data[i].count != 0) {
                        abnormal.push(data[i]);
                    }
                }
                callback(abnormal)
            })
        });
    };
    //统计异常未读总数
    service.getAbnormalUnreadALL = function (callback) {
        var sql = "select count(1) as count,a.unread, a.jsonPK from QC_ABNORMAL a left join QC_ORDER b on a.orderid = b.jsonPK where a.status != '已完成' and b.status = '进行中' group by a.jsonPK";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data))
            var jsonStr = '';
            var abnCount = 0;
            for (var i in data) {
                if(data[i].unread == "T") {
                    abnCount++;
                }
                jsonStr += ("'" + data[i].jsonPK + "'");
                if (i != data.length - 1) {
                    jsonStr += ",";
                }
            }
            data.count = abnCount;
            callback(jsonStr, data)
        })
    }

    // 获取未读异常备注
    service.getAbnormalRemarkUnreadALL = function (jsonPK, callback) {
        var sql = "select count(1) as count from QC_REMARK where objID in(" + jsonPK + ") and unread = 'T'";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data))
            callback(data)
        })
    }
    //统计异常备注未读信息
    service.getAbnormalRemarkUnreadCount = function (callback) {
        var sql = "select a.jsonPK as jsonPK , a.unread as unread , count(1) as count from QC_REMARK m join QC_ABNORMAL a on a.jsonPK = m.objID where m.unread = 'T' group by a.jsonPK";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data));
            callback(data)
        });
    };
    //异常备注未读改为已读
    service.updateAbnormalRemarkUnreadMake = function (abnormalID, callback) {
        var sql = "update QC_REMARK set unread = 'F' where objID = ?";
        mc.execSQL(sql, [abnormalID], function (data) {
            //alert(JSON.stringify(data)); 
            callback(data)
        });
    };
    //异常未读改为已读
    service.updateAbnormalUnreadMake = function (abnormalID, callback) {
        var sql = "update QC_ABNORMAL set unread = 'F' where jsonPK = ?";
        mc.execSQL(sql, [abnormalID], function (data) {
            //alert(JSON.stringify(data))
            callback(data)
        })
    };
    //统计流程备注未读信息
    service.getProcessUnreadCount = function (callback) {
        var sql = "select a.jsonPK,a.unread, c.status from QC_REMARK a left join qc_process b on a.objid = b.jsonPK left join qc_order c on c.jsonPK = b.orderid where a.type = 1 and c.status = '进行中';";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data))
            var objStr = '';
            var num = 0;
            for (var i in data) {
                if (data[i].jsonPK) {
                    objStr += ("'" + data[i].jsonPK + "'");

                    if (i != data.length - 1) {
                        objStr += ",";
                    }
                    if (data[i].unread == 'T') {
                        num++;
                    }
                }
            }
            if (objStr[objStr.length - 1] == ",") {
                objStr = objStr.substr(0, objStr.length - 1);
            }
            service.getProcessRemarkUnreadCount(objStr, function (remark) {
                data.count = num + parseInt(remark[0].count);
                callback(data)
            });
        })
    };
    // 获取未读流程备注
    service.getProcessRemarkUnreadCount = function (objStr, callback) {
        var sql = "select count(1) as count from QC_REMARK where unread = 'T' and objID in (" + objStr + ")";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data));
            callback(data)
        })
    };
    //全部历史计数
    service.getAllHistoryUnreadCount = function (callback) {
        var sql = "select count(1) as count from QC_DYNAMIC where unread = 'T'";
        mc.execSQL(sql, [], function (data) {
            if (data[0].count > 0) {
                callback(true);
            } else {
                callback(false);
            }
        })
    };
    //全部历史未读改已读
    service.updateHistoryUnreadMark = function (callback) {
        var sql = "update QC_DYNAMIC set unread = 'F'";
        mc.execSQL(sql, [], callback)
    };
    //流程备注未读改已读
    service.updateProcessUnreadMark = function (str, callback) {
        var sql = "update QC_REMARK set unread = 'F' where jsonPK in (" + str + ");";
        mc.execSQL(sql, [], function () {
            service.updateProcessRemarkUnreadMark(str, function () {
                callback()
            });
        })
    };
    service.updateProcessRemarkUnreadMark = function (str, callback) {
        var sql = "update QC_REMARK set unread = 'F' where objID in (" + str + ");";
        mc.execSQL(sql, [], function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        })
    };
    //统计流程备注未读信息
    service.getOrderRemarkAllUnreadCount = function (sellerID, callback) {
        var sqlOrder = "select jsonPK from qc_order where sellerID = ? and status = '进行中'";
        mc.execSQL(sqlOrder, [sellerID], function (dataOrder) {
            //alert(JSON.stringify(dataOrder));
            var sql = "select count(1) as count,a.jsonPK,a.orderNo from qc_order a left join qc_process b on a.jsonPK =  b.orderid left join qc_remark c on b.jsonpk = c.objid  where a.sellerID=? and c.unread = 'T' group by a.jsonpk ;";
            mc.execSQL(sql, [sellerID], function (data) {
                //alert(JSON.stringify(data));
                service.getOrderRemarkAllUnreadCall(sellerID, function (datas) {
                    for (var z in dataOrder) {
                        var num = 0;
                        for (var i in data) {
                            if(data[i].jsonPK == dataOrder[z].jsonPK) {
                                num = parseInt(data[i].count);
                            }
                        }
                        for (var j in datas) {
                            if (dataOrder[z].jsonPK == datas[j].jsonPK) {
                                num += parseInt(datas[j].count);
                                //num=parseInt(datas[j].count);
                            }
                        }
                        dataOrder[z].count = num;
                    }
                    callback(dataOrder);
                });
            });
        })

    };
    service.getOrderRemarkAllUnreadCall = function (sellerID, callback) {
        var sql = "select count(1) as count,a.jsonpk,d.unread as un from qc_order a left join qc_process b on a.jsonpk =  b.orderid left join qc_remark c on b.jsonpk = c.objid left join qc_remark d on c.jsonpk = d.objid where a.sellerID=? and d.unread='T' group by a.jsonpk;";
        mc.execSQL(sql, [sellerID], function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        })
    };
    service.getOrderAbnormalAllUnreadCount = function (sellerID, callback) {
        var sqlOrder = "select jsonPK from qc_order where sellerID = ? and status = '进行中'";
        mc.execSQL(sqlOrder, [sellerID], function (dataOrder) {
            var sql = "select count(1) as count,a.jsonpk from qc_order a left join qc_abnormal b on a.jsonpk =  b.orderid  where a.sellerID=? and b.unread='T' and b.status = '进行中' group by a.jsonpk;";
            mc.execSQL(sql, [sellerID], function (data) {
                //alert(JSON.stringify(data));
                service.getOrderAbnormalAllUnreadCall(sellerID, function (datas) {
                   /* for (var i in data) {
                        var num = parseInt(data[i].count) ? parseInt(data[i].count) : 0;
                        for (var j in datas) {
                            if (data[i].jsonPK == datas[j].jsonPK) {
                                num += parseInt(datas[j].count);
                            }
                        }
                        data[i].count = num;
                    }*/
                    for (var z in dataOrder) {
                        var num = 0;
                        for (var i in data) {
                            if(data[i].jsonPK == dataOrder[z].jsonPK) {
                                num = parseInt(data[i].count);
                            }
                        }
                        for (var j in datas) {
                            if (dataOrder[z].jsonPK == datas[j].jsonPK) {
                                num += parseInt(datas[j].count);
                                //num=parseInt(datas[j].count);
                            }
                        }
                        dataOrder[z].count = num;
                    }
                    callback(dataOrder);
                })
            });
        });
    };
    service.getOrderAbnormalAllUnreadCall = function (sellerID, callback) {
        var sql = "select count(1) as count,a.jsonpk from qc_order a left join qc_abnormal b on a.jsonpk =  b.orderid left join qc_remark c on b.jsonpk = c.objid  where a.sellerID=? and c.unread='T' and b.status = '进行中' group by a.jsonpk ";
        mc.execSQL(sql, [sellerID], function (data) {
            //alert(JSON.stringify(data));
            callback(data);
        })
    };
    //订单异常未读
    service.getOrderAbnormalRemarkUnreadCount = function (orderID, callback) {
        var sql = "select count(1) as count from qc_abnormal a left join qc_remark b on a.jsonPK = b.objid where a.orderID=? and b.unread = 'T'";
        mc.execSQL(sql, [orderID], function (data) {
            //alert(JSON.stringify(data));
            data.count = data[0].count;
            service.getOrderAbnormal(orderID, function (abnormal) {
                //alert(JSON.stringify(abnormal));
                data.count = parseInt(data.count) + parseInt(abnormal[0].count);
                callback(data)
            })
        })
    };
    service.getAbnormalRemarkUnreadCountInDetail = function (orderID, callback) {
        var sql = "SELECT count(1) AS count, a.jsonPK FROM qc_abnormal a LEFT JOIN qc_remark b ON a.jsonPK = b.objid WHERE	a.orderID = ? AND b.unread = 'T' AND a.status = '进行中' GROUP BY a.jsonPK;";
        mc.execSQL(sql, [orderID], function (data) {
            callback(data);
        });
    };
    service.getOrderAbnormal = function (orderID, callback) {
        var sql = "select count(1) as count from qc_abnormal where unread='T' and orderID=? and status = '进行中';";
        mc.execSQL(sql, [orderID], function (data) {
            //alert(JSON.stringify(data))
            callback(data)
        });
    };
    //订单流程未读
    service.getOrderPorcessRemarkUnreadCount = function (orderID, callback) {
        var sql = "select count(1) as count from qc_process a left join qc_remark b on a.jsonPK = b.objID left join qc_remark c on b.jsonPK = c.objID where a.orderID=? and c.unread='T'";
        mc.execSQL(sql, [orderID], function (data) {
            service.getOrderProcess(orderID, function (process) {
                //alert(JSON.stringify(process))
                var count = parseInt(data[0].count) + parseInt(process[0].count);
                callback(count)
            })
        })
    };

    // 获取流程
    service.getOrderProcess = function (orderID, callback) {
        var sql = "select count(1) as count from qc_process a left join qc_remark b on a.jsonPK = b.objID where b.unread='T' and a.orderID=?";
        mc.execSQL(sql, [orderID], function (data) {
            //alert(JSON.stringify(data))
            callback(data)
        })
    };

    // 获取消息
    service.getMessageData = function (options, callback) {
        var sql = "select a.*,b.attachment as orderAttachment,b.orderNo as orderNo,b.itemNo as itemNo from QC_MESSAGE a join QC_ORDER b on a.orderID = b.jsonPK limit ?,?";
        var page = options.currentPage;
        var size = options.pageSize;
        var params = [(page - 1) * size, size];
        mc.execSQL(sql, params, function (data) {
            //alert(JSON.stringify(data))
            callback(data);
        })
    };

    // 修改密码
    service.modPwd = function (options, callback) {
        var requestUrl = service.url + "/modifyPassword";
        mc.beginAsyncAjax(requestUrl, options, "POST", function (data) {
            callback(data);
        });
    }
})();